import '../public/index'
import url from '../data/url'
import request from '../data/request'
import {tableConfig, header} from '../data/config'
import store from 'store'
import {getUrlParam, fetch} from '../data/util'

const base = url.base

let app = new Vue({
  el: '#app',
  data: {
    id: false,
    justLook: false,
    editor: null,
    sendToSave: {
      id: null, //协议配置id	如果为空表示新增
      bankId: null,  //银行编码
      protocolName: null, //协议名称
      state: null,  //是否显示
      content: null,  //协议内容
      displayOrder: null  //显示顺序
    },
    bankList: [],
    agreementInfoShow: {
      bankName: null
    },
    btnMsg: {
      title: "展开",
      isShow: false
    }
  },
  created: function () {
    this.checkId()
  }
  ,
  methods: {
    btnCollapse() {
      this.btnMsg.isShow = !this.btnMsg.isShow
      switch (this.btnMsg.isShow) {
        case false:
          this.btnMsg.title = '展开'
          break
        case true:
          this.btnMsg.title = '收起'
          break
      }
    },
    checkId() {
      if (getUrlParam('id')) {
        this.id = getUrlParam('id')
        this.justLook = getUrlParam('justLook')
        this.editOldAgreement()
      } else {
        this.createNewAgreement()
      }
    },
    editOldAgreement() {
      let app = this
      fetch({
        url: '/admin/bankProtocol/option_detail.json',
        data: {id: this.id}
      }).then(res => {
        let agreementInfo = res.object
        app.sendToSave.id = agreementInfo.id
        app.sendToSave.bankId = agreementInfo.bankId
        app.agreementInfoShow.bankName = agreementInfo.attribute
        app.sendToSave.protocolName = agreementInfo.protocolName
        app.sendToSave.state = agreementInfo.state
        app.sendToSave.content = agreementInfo.content
        app.sendToSave.displayOrder = agreementInfo.displayOrder
        app.initEditor(this.sendToSave.content)
      })
    },
    createNewAgreement() {
      let app = this
      fetch({
        url: '/admin/bankProtocol/facade.json',
        data: {id: this.id}
      }).then(res => {
        console.log(res.bankInfoList)
        app.bankList = app.bankList.concat([...res.bankInfoList])
        console.log('app.bankList:', app.bankList, app)
        app.initEditor()
      })
    },
    initEditor(content) {
      const E = window.wangEditor
      const editor2 = new E('#text-div')
      this.editor = editor2
      editor2.create()
      if (content) {
        content = content.replace(new RegExp(/@!#!#@/g), '\r\n');
        content = content.replace(new RegExp(/@#!#!@/g), "'");
        editor2.txt.html(content)
      }
    },
    saveAgreementInfo() {
      let text = this.editor.txt.html()
      this.sendToSave.content = text
      console.log(this.sendToSave)
      let data = this.sendToSave
      $.ajax({
        type: "POST",
        url: base + '/admin/bankProtocol/addOrUpdate.json',
        dataType: "json",
        data: data,
        headers: {
          myToken: Cookies.get('myToken')
        },
      }).then(res => {
        console.log('res:', res)
        location.href = './index.html'
      })
    },
    cancel: () => {
      location.href = './index.html'
    }
  }
})


